package com.hezhi.project.module.teacher.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hezhi.project.module.teacher.mapper.TeacherMapper;
import com.hezhi.project.module.teacher.domain.Teacher;
import com.hezhi.project.module.teacher.service.ITeacherService;
import com.hezhi.project.module.teacherClasses.domain.TeacherClasses;
import com.hezhi.project.module.teacherClasses.mapper.TeacherClassesMapper;
import com.hezhi.common.support.Convert;

/**
 * 教师 服务层实现
 * 
 * @author hezhi
 * @date 2018-08-17
 */
@Service
public class TeacherServiceImpl implements ITeacherService 
{
	@Autowired
	private TeacherMapper teacherMapper;
	@Autowired
	private TeacherClassesMapper teacherClassesMapper;

	/**
     * 查询教师信息
     * 
     * @param teacherNumber 教师ID
     * @return 教师信息
     */
    @Override
	public Teacher selectTeacherById(String teacherNumber)
	{
	    return teacherMapper.selectTeacherById(teacherNumber);
	}
	
	/**
     * 查询教师列表
     * 
     * @param teacher 教师信息
     * @return 教师集合
     */
	@Override
	public List<Teacher> selectTeacherList(Teacher teacher)
	{
	    return teacherMapper.selectTeacherList(teacher);
	}
	
    /**
     * 新增教师
     * 
     * @param teacher 教师信息
     * @return 结果
     */
	@Override
	public int insertTeacher(Teacher teacher)
	{
	    return teacherMapper.insertTeacher(teacher);
	}
	
	/**
     * 修改教师
     * 
     * @param teacher 教师信息
     * @return 结果
     */
	@Override
	public int updateTeacher(Teacher teacher)
	{
	    return teacherMapper.updateTeacher(teacher);
	}
	
	/**
     * 保存教师
     * 
     * @param teacher 教师信息
     * @return 结果
     */
	@Override
	@Transactional
	public int saveTeacher(Teacher teacher)
	{
		boolean ifNew = teacher.getIfNew();
		int rows = 0;
		if (ifNew)
        {
			rows = teacherMapper.insertTeacher(teacher);
			//新增教师与班级对应关系
			TeacherClasses teacherClasses = teacher.getTeacherClasses();
			teacherClasses.setTeacherNumber(teacher.getTeacherNumber());
			teacherClassesMapper.insertTeacherClasses(teacherClasses);
		}
		else
        {
			rows = teacherMapper.updateTeacher(teacher);
			TeacherClasses teacherClasses = teacher.getTeacherClasses();
			teacherClasses.setTeacherNumber(teacher.getTeacherNumber());
			teacherClassesMapper.updateTeacherClasses(teacherClasses);
		}
		return rows;
	}
	
	/**
     * 删除教师对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteTeacherByIds(String ids)
	{
		int i = teacherMapper.deleteTeacherByIds(Convert.toStrArray(ids));
		teacherClassesMapper.deleteTeacherClassesByTeacherNumbers(Convert.toStrArray(ids));
		return i;
	}

	@Override
	public Teacher selectTeacherByOpenId(Teacher teacher) {
		return teacherMapper.selectTeacherByOpenId(teacher);
	}

	@Override
	public int updateTeacherByTelphone(Teacher teacher) {
		return teacherMapper.updateTeacherByTelphone(teacher);
	}
	
}
